package com.yiqi.zhiyuan.feature.order.view;


import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.utils.DensityUtil;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.enums.OrderFlagType;
import com.yiqi.zhiyuan.entity.enums.OrderType;
import com.yiqi.zhiyuan.entity.enums.OrderUseType;
import com.yiqi.zhiyuan.entity.vo.OrderVO;
import com.yiqi.zhiyuan.feature.order.contract.OrderContract;
import com.yiqi.zhiyuan.feature.order.dependencies.order.DaggerOrderComponent;
import com.yiqi.zhiyuan.feature.order.dependencies.order.OrderPresenterModule;
import com.yiqi.zhiyuan.feature.order.view.adapter.OrderAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

public class OrderListFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, OrderContract.View, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, View.OnClickListener {

    @BindView(R.id.rv_order)
    RecyclerView mRvOrder;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;

    private int mType;
    private OrderAdapter mAdapter;
    private EmptyView mEmptyView;

    private int mPageIndex = 1;
    private int mPageSize = 10;

    @Inject
    OrderContract.Presenter mPresenter;


    public static OrderListFragment getInstance(int title) {
        OrderListFragment sf = new OrderListFragment();
        sf.mType = title;
        return sf;
    }

    @Override
    public void onStart() {
        super.onStart();
        loadData();
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_order_list;
    }

    @Override
    protected void initInjector() {
        DaggerOrderComponent.builder().orderPresenterModule(new OrderPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initAdapter();
    }

    @Override
    protected void initData() {


    }

    @Override
    protected void initEvent() {

    }

    private void initAdapter() {
        mSmartRefreshLayout.setEnableLoadMore(false);
        mSmartRefreshLayout.setOnRefreshListener(this);
        mEmptyView = new EmptyView(mRvOrder);
        mEmptyView.setErrorButtonClickListener(this);
        mRvOrder.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvOrder.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(DensityUtil.dp2px(10)));
        mAdapter = new OrderAdapter(mType);
        mAdapter.bindToRecyclerView(mRvOrder);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnLoadMoreListener(this, mRvOrder);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        OrderVO orderVO = mAdapter.getItem(position);
        Intent intent = null;
        if (orderVO!=null) {
            if (orderVO.getUseCarType() == 1) {//接机单
                if (orderVO.getOrderStatus() == 4) {//结束行程
                    intent = new Intent(getActivity(), EndTaskActivity.class);
                } else if (orderVO.getOrderStatus() == 5) {//已完成
                    if (orderVO.getDriverOrder().getIsConfirm() == 0) {//是否确认
                        intent = new Intent(getActivity(), ScheduleConfirmedActivity.class);
                    }else {
                        intent = new Intent(getActivity(), OrderEndActivity.class);
                    }

                }else if (orderVO.getOrderStatus() == 0) {//已取消

                } else if (orderVO.getOrderStatus() == 3) {//已取消
                    intent = new Intent(getActivity(), OrderInfoActivity.class);
                } else {//其他状态
                    intent = new Intent(getActivity(), OrderTripingActivity.class);
                }
            } else if (orderVO.getUseCarType() == 0) {//用车
                if (orderVO.getOrderType() == 0) {//现在
                    if (orderVO.getOrderStatus() == 4) {//结束行程
                        intent = new Intent(getActivity(), EndTaskActivity.class);
                    } else if (orderVO.getOrderStatus() == 5) {//已完成
                        if (orderVO.getDriverOrder().getIsConfirm() == 0) {
                            intent = new Intent(getActivity(), ScheduleConfirmedActivity.class);
                        }else {
                            intent = new Intent(getActivity(), OrderEndActivity.class);
                        }
                    } else if (orderVO.getOrderStatus() == 0) {//已取消

                    } else {//其他状态
                        intent = new Intent(getActivity(), OrderInfoActivity.class);
                    }
                } else if (orderVO.getOrderType() == 1) {//预约
                    if (orderVO.getOrderStatus() == 4) {//结束行程
                        intent = new Intent(getActivity(), EndTaskActivity.class);
                    } else if (orderVO.getOrderStatus() == 5) {//已完成

                        if (orderVO.getDriverOrder().getIsConfirm() == 0) {
                            intent = new Intent(getActivity(), ScheduleConfirmedActivity.class);
                        }else {
                            intent = new Intent(getActivity(), OrderEndActivity.class);
                        }
                    }else if (orderVO.getOrderStatus() == 3) {//已取消
                        intent = new Intent(getActivity(), OrderInfoActivity.class);
                    } else if (orderVO.getOrderStatus() == 0) {//已取消

                    } else {//其他状态
                        intent = new Intent(getActivity(), OrderTripingActivity.class);
                    }

                } else if (orderVO.getOrderType() == 2) {//代叫
                    if (orderVO.getOrderFlag() == 0) {//现在出发
                        if (orderVO.getOrderStatus() == 4) {//结束行程
                            intent = new Intent(getActivity(), EndTaskActivity.class);
                        } else if (orderVO.getOrderStatus() == 5) {//已完成
                            if (orderVO.getDriverOrder().getIsConfirm() == 0) {
                                intent = new Intent(getActivity(), ScheduleConfirmedActivity.class);
                            }else {
                                intent = new Intent(getActivity(), OrderEndActivity.class);
                            }
                        } else if (orderVO.getOrderStatus() == 0) {//已取消

                        } else {//其他状态
                            intent = new Intent(getActivity(), OrderInfoActivity.class);
                        }
                    } else if (orderVO.getOrderFlag() == 1) {//预约时间
                        if (orderVO.getOrderStatus() == 4) {//结束行程
                            intent = new Intent(getActivity(), EndTaskActivity.class);
                        } else if (orderVO.getOrderStatus() == 5) {//已完成
                            if (orderVO.getDriverOrder().getIsConfirm() == 0) {
                                intent = new Intent(getActivity(), ScheduleConfirmedActivity.class);
                            }else {
                                intent = new Intent(getActivity(), OrderEndActivity.class);
                            }

                        } else if (orderVO.getOrderStatus() == 3) {//已取消
                            intent = new Intent(getActivity(), OrderInfoActivity.class);
                        }else if (orderVO.getOrderStatus() == 0) {//已取消

                        } else {//其他状态
                            intent = new Intent(getActivity(), OrderTripingActivity.class);
                        }
                    }
                }

            } else if (orderVO.getUseCarType() == 2) {
                if (orderVO.getOrderType() == 0) {//现在出发
                    if (orderVO.getOrderStatus() == 4) {//结束行程
                        intent = new Intent(getActivity(), EndTaskActivity.class);
                    } else if (orderVO.getOrderStatus() == 5) {//已完成
                        if (orderVO.getDriverOrder().getIsConfirm() == 0) {
                            intent = new Intent(getActivity(), ScheduleConfirmedActivity.class);
                        }else {
                            intent = new Intent(getActivity(), OrderEndActivity.class);
                        }
                    } else if (orderVO.getOrderStatus() == 0) {//已取消

                    } else {//其他状态
                        intent = new Intent(getActivity(), OrderInfoActivity.class);
                    }
                } else if (orderVO.getOrderType() == 1) {//预约时间
                    if (orderVO.getOrderStatus() == 4) {//结束行程
                        intent = new Intent(getActivity(), EndTaskActivity.class);
                    } else if (orderVO.getOrderStatus() == 5) {//已完成
                        if (orderVO.getDriverOrder().getIsConfirm() == 0) {
                            intent = new Intent(getActivity(), ScheduleConfirmedActivity.class);
                        }else {
                            intent = new Intent(getActivity(), OrderEndActivity.class);
                        }

                    } else if (orderVO.getOrderStatus() == 3) {//已取消
                        intent = new Intent(getActivity(), OrderInfoActivity.class);
                    }else if (orderVO.getOrderStatus() == 0) {//已取消

                    } else {//其他状态
                        intent = new Intent(getActivity(), OrderTripingActivity.class);
                    }
                }else if (orderVO.getOrderType() == 2){
                    if (orderVO.getOrderFlag() == 0) {//现在出发
                        if (orderVO.getOrderStatus() == 4) {//结束行程
                            intent = new Intent(getActivity(), EndTaskActivity.class);
                        } else if (orderVO.getOrderStatus() == 5) {//已完成
                            if (orderVO.getDriverOrder().getIsConfirm() == 0) {
                                intent = new Intent(getActivity(), ScheduleConfirmedActivity.class);
                            }else {
                                intent = new Intent(getActivity(), OrderEndActivity.class);
                            }
                        } else if (orderVO.getOrderStatus() == 0) {//已取消

                        } else {//其他状态
                            intent = new Intent(getActivity(), OrderInfoActivity.class);
                        }
                    } else if (orderVO.getOrderFlag() == 1) {//预约时间
                        if (orderVO.getOrderStatus() == 4) {//结束行程
                            intent = new Intent(getActivity(), EndTaskActivity.class);
                        } else if (orderVO.getOrderStatus() == 5) {//已完成
                            if (orderVO.getDriverOrder().getIsConfirm() == 0) {
                                intent = new Intent(getActivity(), ScheduleConfirmedActivity.class);
                            }else {
                                intent = new Intent(getActivity(), OrderEndActivity.class);
                            }

                        }else if (orderVO.getOrderStatus() == 3) {//已取消
                            intent = new Intent(getActivity(), OrderInfoActivity.class);
                        } else if (orderVO.getOrderStatus() == 0) {//已取消

                        } else {//其他状态
                            intent = new Intent(getActivity(), OrderTripingActivity.class);
                        }
                    }
                }
            }
        }
        if (intent != null) {
            intent.putExtra("order_info", mAdapter.getItem(position));
            startActivity(intent);
        }
    }

    @Override
    public void showOrderListSuccess(List<OrderVO> data) {
        mSmartRefreshLayout.finishRefresh();
        setData(true, data);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showOrderListError(String errorMsg) {
        mSmartRefreshLayout.finishRefresh();
        mAdapter.setNewData(null);
        mAdapter.setEmptyView(mEmptyView.getErrorView(errorMsg));
    }

    @Override
    public void showOrderListMoreSuccess(List<OrderVO> data) {
        setData(false, data);
    }

    @Override
    public void showOrderListMoreError(String errorMsg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showOrderListEmpty() {
        mSmartRefreshLayout.finishRefresh();
        mAdapter.setNewData(null);
        mAdapter.setEmptyView(mEmptyView.getEmptyView());

    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        refresh();
    }

    private void refresh() {
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        loadData();
    }

    private void loadMore() {
        mPresenter.doSelectOrderListMore(mType, mPageIndex);
    }

    private void setData(boolean isRefresh, List data) {
        mPageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < mPageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    @Override
    public void onClick(View v) {
        loadData();
    }

    private void loadData() {
        mPageIndex = 1;
        mPresenter.doSelectOrderList(mType, mPageIndex);
    }
}
